<?php
/**
 * @package		Joomla.Site
 * @subpackage	Contact
 * @copyright	Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

jimport('joomla.application.component.controllerform');

class ContactControllerContact extends JControllerForm
{
	public function getModel($name = '', $prefix = '', $config = array('ignore_request' => true))
	{
		return parent::getModel($name, $prefix, array('ignore_request' => false));
	}

	public function submit()
	{
		// Check for request forgeries.
		JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

		// Initialise variables.
		$app	= JFactory::getApplication();
		$model	= $this->getModel('contact');
		$params = JComponentHelper::getParams('com_contact');
		$stub	= JRequest::getString('id');
		$id		= (int)$stub;

		// Get the data from POST
		//$data = JRequest::getVar('jform', array(), 'post', 'array');
			$data['name'] = JRequest::getVar('name');
			$data['company'] = JRequest::getVar('company');
			$data['phone'] = JRequest::getVar('phone');
			$data['email'] = JRequest::getVar('email');
			$data['message'] = JRequest::getVar('message');
		$contact = $model->getItem($id);


		$params->merge($contact->params);

		// Check for a valid session cookie
		if($params->get('validate_session', 0)) {
			if(JFactory::getSession()->getState() != 'active'){
				JError::raiseWarning(403, JText::_('COM_CONTACT_SESSION_INVALID'));

				// Save the data in the session.
				$app->setUserState('com_contact.contact.data', $data);

				// Redirect back to the contact form.
				$this->setRedirect(JRoute::_('index.php?option=com_contact&view=contact&id='.$stub, false));
				return false;
			}
		}

		// Contact plugins
		JPluginHelper::importPlugin('contact');
		$dispatcher	= JDispatcher::getInstance();

		// Send the email
		$sent = false;
		if (!$params->get('custom_reply')) {
			$sent = $this->_sendEmail($data, $contact);
		}

		// Set the success message if it was a success
		if (!($sent instanceof Exception)) {
			$msg = JText::_('COM_email_THANKS');
		} else {
			$msg = '' ;
		}

		// Flush the data from the session
		$app->setUserState('com_contact.contact.data', null);

		// Redirect if it is set in the parameters, otherwise redirect back to where we came from
		if ($contact->params->get('redirect')) {
			$this->setRedirect($contact->params->get('redirect'), $msg);
		} else {
			//$this->setRedirect(JRoute::_('index.php?option=com_contact&view=contact&id='.$stub, false), $msg);
			$this->setRedirect(JRoute::_('index.php?option=com_contact&view=contact&complete=1&id='.$stub));
		}

		return true;
	}

	private function _sendEmail($data, $contact)
	{
			$app		= JFactory::getApplication();
			$params 	= JComponentHelper::getParams('com_contact');
			if ($contact->email_to == '' && $contact->user_id != 0) {
				$contact_user = JUser::getInstance($contact->user_id);
				$contact->email_to = $contact_user->get('email');
			}
			$mailfrom	= $app->getCfg('mailfrom');
			$fromname	= $app->getCfg('fromname');
			$sitename	= $app->getCfg('sitename');
			$copytext 	= JText::sprintf('COM_CONTACT_COPYTEXT_OF', $contact->name, $sitename);

			$name		= $data['name'];
			$company	= $data['company'];
			$phone		= $data['phone'];
			$email		= $data['email'];
			$message	= $data['message'];
			//$body		= $data['contact_message'];

			// Prepare email body
			$prefix = JText::sprintf('COM_CONTACT_ENQUIRY_TEXT', JURI::base());
			//$body	= $prefix."\n".$name.' <'.$email.'>'."\r\n\r\n".stripslashes($body);
			$body = "Name : ". $name . "<br>";
			$body .= "Company : ". $company . "<br>";
			$body .= "Phone No. : ". $phone. "<br>";
			$body .= "E-mail : ". $email. "<br>";
			$body .= "Message : ". $message;
			$mail = JFactory::getMailer();
			$mail->isHTML(true);
			$mail->addRecipient($contact->email_to);
			$mail->addRecipient('pernille.kornbeck@logpoint.com');
			//$mail->addRecipient("koolguyhx1402@gmail.com");
			$mail->addReplyTo(array($email, $name));
			$mail->setSender(array($mailfrom, $fromname));
			//$mail->setSubject($sitename.': '.$subject);
			$mail->setSubject("[".$sitename."] - ".$name." has just sent a message to you");
			$mail->setBody($body);
			$sent = $mail->Send();

			//If we are supposed to copy the sender, do so.

			// check whether email copy function activated
			if ( array_key_exists('email_copy', $data)  ) {
				$copytext		= JText::sprintf('COM_CONTACT_COPYTEXT_OF', $contact->name, $sitename);
				$copytext		.= "\r\n\r\n".$body;
				$copysubject	= JText::sprintf('COM_CONTACT_COPYSUBJECT_OF', $subject);

				$mail = JFactory::getMailer();
				$mail->addRecipient($email);
				$mail->addReplyTo(array($email, $name));
				$mail->setSender(array($mailfrom, $fromname));
				$mail->setSubject($copysubject);
				$mail->setBody($copytext);
				$sent = $mail->Send();
			}

			return $sent;
	}
}
